package com.songlanyun.modules.withdraw.controller.api;

import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.withdraw.entity.Withdraw;
import com.songlanyun.modules.withdraw.model.dto.CommissionToBalanceDTO;
import com.songlanyun.modules.withdraw.model.dto.UserWithdrawDTO;
import com.songlanyun.modules.withdraw.model.vo.AccountWalletVO;
import com.songlanyun.modules.withdraw.service.WithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 提现列表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-20 17:47:55
 */
@RestController
@RequestMapping("api/withdraw")
public class WithdrawApiController extends AbstractApiController {
    @Autowired
    private WithdrawService withdrawService;


    /**
     * 会员查询自己的提现记录分页列表
     */
    @GetMapping("/memberPage")
    public R memberPage(@RequestParam Map<String, Object> params) {
        params.put("user_id", getUserId(true));
        PageUtils page = withdrawService.memberPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 会员查询自己的提现记录
     */
    @GetMapping("/memberList")
    public R memberList() {
        List<Withdraw> list = withdrawService.memberList(getUserId(true));

        return R.ok().put("list", list);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        Withdraw withdraw = withdrawService.getById(id);

        return R.ok().put("data", withdraw);
    }

    /**
     * 会员分佣金申请提现
     *
     * @return
     */
    @RepeatSubmit(value = "3")
    @PostMapping("/memberWithdraw")
    public R memberWithdraw(@RequestBody UserWithdrawDTO dto) {
        //会员id
        Long userId = getUserId(true);
        ValidatorUtils.validateEntity(dto);

        withdrawService.memberWithdraw(dto, userId);

        return R.ok();
    }


    /**
     * 会员提现处钱包的信息
     */
    @GetMapping("/getWalletInfo")
    public R getWalletInfo() {
        AccountWalletVO vo = withdrawService.getWalletInfo(getUserId(false));
        return R.ok().put("data", vo);
    }

    /**
     * 佣金转存余额
     */
    @RepeatSubmit("2")
    @PostMapping("/commission-to-balance")
    public R commissionToBalance(@RequestBody CommissionToBalanceDTO dto) {
        withdrawService.commissionToBalance(getUserId(true), dto);
        return R.ok();
    }

    /**
     * 会员提现数
     */
    @GetMapping("/member/count")
    public R count() {
        Long userId = getUserId(false);
        if (ObjectUtil.isNull(userId) || userId == 0L) {
            return R.ok().put("count", 0);
        }
        Integer count = withdrawService.count(userId);
        return R.ok().put("count", count);
    }
}
